//
//  IMImageMessageTableViewCell.m
//  MLIM
//
//  Created by molon on 15/6/29.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMImageMessageTableViewCell.h"
#import "IMMessageBackgroundImageView.h"
#import <MLKit.h>
#import "IMCommon.h"
#import "IMUserAvatarImageView.h"

#define kMaxImageHeight 120.0f
#define kMaxWidthRadioOfImageView (170.0f/320.0f)

#define MSG ((IMImageMessage*)self.imMessage)


static CGRect CGRectCenterRectForResizableImage(UIImage *image) {
    return CGRectMake(image.capInsets.left/image.size.width, image.capInsets.top/image.size.height, (image.size.width-image.capInsets.right-image.capInsets.left)/image.size.width, (image.size.height-image.capInsets.bottom-image.capInsets.top)/image.size.height);
}

@interface IMImageMessageTableViewCell()

@property (nonatomic, strong) UIView *overlayView;

@end

@implementation IMImageMessageTableViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)resetWithIsInit:(BOOL)isInit
{
    [super resetWithIsInit:isInit];
    
    if (isInit) {
        self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.backgroundImageView addSubview:self.overlayView];
        
        //给其加个点击事件吧
        UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openImage)];
        [self.backgroundImageView addGestureRecognizer:tapG];
    }
}

#pragma mark - event
- (void)openImage
{
    if (MSG.imageURL) {
        if (self.showPictureBlock) {
            self.showPictureBlock(self);
        }
    }
}

#pragma mark - getter
- (UIView *)overlayView
{
    if (!_overlayView) {
        _overlayView = [UIView new];
        _overlayView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.20f];
    }
    return _overlayView;
}

#pragma mark - setter
- (void)setImMessage:(IMMessage *)imMessage
{
    NSAssert(!imMessage||[imMessage isKindOfClass:[IMImageMessage class]], @"IMImageMessageCell只接受IMImageMessage对象");
    
    [super setImMessage:imMessage];
    
    //设置mask
    UIImage *maskImage = [self currentMaskImage];
    CALayer *mask = [CALayer layer];
    mask.contents = (id)maskImage.CGImage;
    mask.contentsScale = [UIScreen mainScreen].scale;
    
    //设置拉伸属性，根据capInsets，找到可拉伸的矩形
    CGRect capRect = CGRectCenterRectForResizableImage(maskImage);
    
    mask.contentsCenter = capRect;
    self.backgroundImageView.layer.mask = mask;
    
    [self.backgroundImageView sd_setImageWithURL:MSG.imageURL placeholderImage:[UIImage imageWithPureColor:[UIColor lightGrayColor]] options:SDWebImageRetryFailed];
    
    [self setNeedsLayout];
}

#pragma mark - height
+ (CGFloat)heightOfMessage:(IMMessage*)imMessage maxWidth:(CGFloat)maxWidth
{
    IMImageMessage *message = CHILD(IMImageMessage, imMessage);
    
    CGFloat heightForNotDisplayDate = kMaxImageHeight+kIMMessageCellBottomPadding;
    if (message.imageHeight!=0&&message.imageWidth!=0) {
        heightForNotDisplayDate = (message.imageHeight<kMaxImageHeight?message.imageHeight:kMaxImageHeight)+kIMMessageCellBottomPadding;
    }
    
    if (message.displayDate) {
        return heightForNotDisplayDate+kDateLabelHeight+kDateLabelYMargin*2;
    }
    return heightForNotDisplayDate;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat height = kMaxImageHeight;
    CGFloat width = kMaxWidthRadioOfImageView*self.frameWidth;
    
    if (MSG.imageHeight!=0&&MSG.imageWidth!=0) {
//        width = MIN(MSG.imageWidth, width);
//        height = MIN(width/MSG.imageWidth*MSG.imageHeight,height);
        
        //TODO: 需要考虑的是如果图片的比例很诡异，
        //例如宽1000，高10的话，可能需要考虑个最小height，聊天消息列表上肯定是不希望连点击都点不到
        //同理宽高反过来也一样，重做时候搞
        height = MIN(MSG.imageHeight, height);
        width = MIN(height/MSG.imageHeight*MSG.imageWidth,width);
    }
    
    //左和右
    if (IMISME(self.imMessage.clientId)) {
        self.backgroundImageView.frame = CGRectMake(self.avatarImageView.frameX - kElementXSpacing/2 - width, self.avatarImageView.frameY, width, height);
    }else{
        self.backgroundImageView.frame = CGRectMake(self.avatarImageView.frameRight + kElementXSpacing/2, self.avatarImageView.frameY, width, height);
    }
    self.overlayView.frame = self.backgroundImageView.bounds;
    
    self.backgroundImageView.layer.mask.frame = self.backgroundImageView.bounds;
    
    [self layoutEnd];
}

#pragma mark - helper
- (UIImage*)currentMaskImage
{
    if (IMISME(self.imMessage.clientId)) {
        return [IMIMG(@"Sender背景") resizableImageWithCapInsets:IMCONFIGM.senderMessageBackgoundCapInsets];
    }
    
    return [IMIMG(@"Receiver背景") resizableImageWithCapInsets:IMCONFIGM.recevierMessageBackgoundCapInsets];
}

#pragma mark - outcall
- (void)changeBackgroundImageToDark:(BOOL)dark
{
    self.overlayView.hidden = !dark;
}

@end
